<template>
	<view class="content top">
		<view class="" v-if="show">
			<view class="top_center">
				本程序需获取您的位置才能使用
				
			</view>
			<u-button type="primary" text="重新获取位置" @click="getTick"></u-button>
		
		</view>
	</view>
</template>

<script>
	let jweixin = require('jweixin-module');
	export default {
		data() {
			return {
				show: false,
				user: {
					
				},
				userInfo: {}
			}
		},
		onLoad() {
			// var _this = this
			// uni.showLoading({
			//  	title: '授权中',
			//  	mask: true
			//  });
			// console.log("进入开始")
			// var tempopenid  = uni.getStorageSync('OPENID')
			// console.log("获取当前页面的url")
			// var appid = 'wx90023bc01d0bad80'
			// console.log("判断appid 是否存在url中"+appid)
			// if (appid == null) {
			// 	console.log("appid 是空的 然后给一个默认值 ")
			// 	appid = 'wx90023bc01d0bad80'
			// } 
			// uni.setStorageSync("appid",appid);
			// console.log("appid:" + appid)
			// //获取当前页面的url
			// let link = window.location.href;
			// console.log(link,'link');
			// const openid = uni.getStorageSync('OPENID');
			// var urlcode = ''
			// var codeurl = ''
			// if(link.indexOf('code=') != -1){
			// 	let temp = link.split("code=")[1];
			// 	codeurl = temp.split("&")[0];
			// 	console.log(codeurl,'code');
			// 	urlcode = codeurl
			// }
			// console.log("openid:"+openid)
			// console.log("urlcode:"+urlcode)
			// if(openid == '' && urlcode == ''){
			// 	console.log("进入这里1")
			// 	//不存在存储的code 不存在地址参数code 不存在openid
			// 	//存储当前初始页面历史列表数量
			// 	uni.setStorageSync("historyLength",history.length);
			// 	let uri = encodeURIComponent(link);
			// 	let authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`;
			// 	console.log("授权url："+authURL)
			// 	// window.location.href = authURL;
			// 	// 去微信获取code
			// 	// document.location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=STATE#wechat_redirect`);
			// 	document.location.replace(authURL);
			// }else if(urlcode != '' && openid == ''){
			// 	console.log("进入这里2")
			// 	//回调函数已经执行 返回的链接存在code= 地址解析
			// 	//存在地址参数code 不存在openid
			// 	console.log("回调函数已经执行 返回的链接存在code")
			// 	console.log("存在地址参数code 不存在openid")
			// 	console.log(urlcode)
			// 	//请求后端接口获取用户的信息
			// 	// let result = await getWxAuthorize({code,scope:"snsapi_userinfo"});
			// 	uni.request({
			// 		url: '/wxsite/Index/getWxUserInfo',
			// 		method: 'POST',
			// 		header:{
			// 			"Content-Type" : "application/x-www-form-urlencoded"
			// 		},
			// 		data: {
			// 			code: urlcode,
			// 		},
			// 		success: (res) => {
			// 			console.log(res)
			// 			console.log(res.data.code )
			// 			uni.hideLoading()
						
			// 			if (res.data.code == 1) {
			// 				_this.user = res.data.data
			// 				// uni.setStorageSync('openid', openid)
			// 				uni.setStorageSync('OPENID',res.data.data.openid);
			// 				console.log("设置OPENID："+res.data.data.openid)
			// 				tempopenid = res.data.data.openid
			// 				console.log("openid缓存："+uni.getStorageSync('OPENID'))
			// 				console.log("openidtemp："+tempopenid)
			// 				console.log("结束")
			// 				let link = window.location.href;
			// 				console.log('link',link)
			// 				if(link.indexOf('type=') !=-1){
			// 					let temp = link.split("type=")[1];
			// 					let type = temp.split("&")[0];
			// 					console.log(type,'type');
			// 					if(type == '1#/' || type == 1){
			// 						_this.is_user()
									
			// 					}else{
			// 						uni.reLaunch({
			// 							url: '/pages/admin/login'
			// 						})
			// 					}
			// 				}else if(link.indexOf('macno=')!=-1){
			// 					console.log('macno',link)
			// 					let temp = link.split("macno=")[1];
			// 					let macno = temp.split("&")[0];
			// 					// console.log(macno)
			// 					uni.setStorageSync('macno',macno)
			// 					_this.is_user()
			// 				}
			// 				// _this.getTick()
			// 				// window.location.reload()
			// 			} else {
			// 				console.log("设置授权失败")
			// 				return false;
			// 			}
			// 		},
			// 		fail() {
			// 			uni.hideLoading()
			// 			console.log(`后端返回失败 `)
			// 			return false;
			// 		}
			// 	});
			// 	//保存用户的openid
			// }else{
			// 	uni.hideLoading()
			// 	console.log("openid缓存1："+uni.getStorageSync('OPENID'))
			// 	console.log("openidtemp1："+tempopenid)
			// 	console.log("结束1")
			// 	let link = window.location.href;
			// 	if(link.indexOf('type=') != -1){
			// 		let temp = link.split("type=")[1];
			// 		let type = temp.split("&")[0];
			// 		console.log(type,'type');
			// 		if(type == '1#/' || type == 1){
			// 			_this.is_user()
			// 		}else{
			// 			uni.reLaunch({
			// 				url: '/pages/admin/login'
			// 			})
			// 		}
			// 	}else if(link.indexOf('macno=')!=-1){
			// 		console.log('macno',link)
			// 		let temp = link.split("macno=")[1];
			// 		let macno = temp.split("&")[0];
			// 		console.log(macno)
			// 		uni.setStorageSync('macno',macno)
			// 		_this.is_user()
			// 	}
			// }
		},
		methods: {
			is_user(){
				var _this = this
				var _this = this
				uni.request({
					url: '/wxsite/Index/isusergps',
					method: 'POST',
					header:{
						"Content-Type" : "application/x-www-form-urlencoded"
					},
					data: {
						openid: uni.getStorageSync('OPENID'),

					},
					success(res) {
						console.log(res)
						
						if(res.data.code == -1){
							uni.request({
								url: '/wxsite/User/userImgv2',
								method: 'POST',
								header:{
									"Content-Type" : "application/x-www-form-urlencoded"
								},
								data: {
									openid: uni.getStorageSync('OPENID'),
								},
								success: (response) => {
									_this.userInfo = response.data.data
									let link = window.location.href;
									if(link.indexOf('type=') != -1){
										let temp = link.split("type=")[1];
										let type = temp.split("&")[0];
										console.log(type,'type');
										
										if(type == '1#/' || type == 1){
											if(_this.userInfo.is_register == 2){
												uni.reLaunch({
													url: '/pages/index/index'
												})
											}else if(_this.userInfo.is_register == 1){
												uni.reLaunch({
													url: '/pages/register/register'
												})
											}
											
										}else{
											uni.reLaunch({
												url: '/pages/admin/login'
											})
										}
									}else if(link.indexOf('macno=')!=-1){
										
										if(_this.userInfo.is_register == 2){
											uni.reLaunch({
												url: '/pages/register/putIn'
											})
										}else if(_this.userInfo.is_register == 1){
											uni.reLaunch({
												url: '/pages/register/register'
											})
										}
									}
								}
							})
							
							
						}else if(res.data.code == 1){
							_this.getTick()
						}
						
					}
				})
			},
			lat_auto(longitude,lat){
				var _this = this
				uni.request({
					url: '/wxsite/Index/getUserGpsInfo',
					method: 'POST',
					header:{
						"Content-Type" : "application/x-www-form-urlencoded"
					},
					data: {
						openid: uni.getStorageSync('OPENID'),
						lng: longitude,
						lat: lat
					},
					success(res) {
						console.log(res)
						if(res.data.code == 1){
							let link = window.location.href;
							if(link.indexOf('type=') != -1){
								let temp = link.split("type=")[1];
								let type = temp.split("&")[0];
								console.log(type,'type');
								if(type == '1#/' || type == 1){
									console.log('mobile',_this.user.mobile)
									if(_this.userInfo.is_register == 2){
										uni.reLaunch({
											url: '/pages/index/index'
										})
									}else if(_this.userInfo.is_register == 1){
										uni.reLaunch({
											url: '/pages/register/register'
										})
									}
									
								}else{
									uni.reLaunch({
										url: '/pages/admin/login'
									})
								}
							}else if(link.indexOf('macno=')!=-1){
								if(_this.userInfo.is_register == 2){
									uni.reLaunch({
										url: '/pages/register/putIn'
									})
								}else if(_this.userInfo.is_register == 1){
									uni.reLaunch({
										url: '/pages/register/register'
									})
								}
							}
							
						}else if(res.data.code == -1){
							
						}
						
					}
				})
			},
			getTick(){
				var _this = this
				uni.request({
					url: '/wxsite/index/getWxJsApiTicketconfig',
					method: 'POST',
					header:{
						"Content-Type" : "application/x-www-form-urlencoded"
					},
					data: {
						url: location.href,
					},
					success: (res) => {
						const dataInfo = res.data.data;
						if (res.data.code == 1) {
							 let jsApiParameters  = {
								   debug: false, // 开启调试模式,调用的所有api的返回值会在客户端//alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
								   appId: dataInfo.appid, // 必填，公众号的唯一标识
								   timestamp: dataInfo.result.timestamp, // 必填，生成签名的时间戳
								   nonceStr: dataInfo.result.noncestr, // 必填，生成签名的随机串
								   signature: dataInfo.result.signature,// 必填，签名，见附录1
								   jsApiList: ['scanQRCode','getLocation','openLocation','chooseImage','getLocalImgData'],
							   }
							jweixin.config(jsApiParameters);
							  jweixin.ready(function(){
								jweixin.getLocation({
									type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
									success: function (resl) {
									   _this.lat_auto(resl.longitude,resl.latitude);
									   
									},
									fail: function(){
										_this.show = true
									},
									complete(){
										_this.show = true
									}
									
								});
							  });
							// window.location.reload()
						} else {
							console.log("设置授权失败")
							return false;
						}
					},
					fail() {
						uni.hideLoading()
						console.log(`后端返回失败 `)
						return false;
					}
				});
			}
		}
	}
</script>

<style scoped>
	.top{
		margin-top: 85px;
		padding: 10px;
		text-align: center;
	}
	.top_center{
		margin: 20px;
	}
</style>
